<template>
  <div class="pagination">
    <button :disabled="pageNo === 1" @click="$emit('getPageNo', pageNo - 1)">
      上一页
    </button>
    <!-- 什么时候有类名:class="{ active: pageNo === 1 }"   -->
    <button
      v-if="continuesPage.start > 1"
      @click="$emit('getPageNo', 1)"
      :class="{ active: pageNo === 1 }"  
    >
      1
    </button>
    <button
      v-if="continuesPage.start > 1"
      @click="$emit('getPageNo', pageNo - 1)"
    >
      ···
    </button>

    <!-- 中间需要动态改变的地方 -->
    <button
      v-for="item in continuesPage.newArr"
      :key="item"
      @click="$emit('getPageNo', item)"
      :class="{ active: pageNo === item }"
    >
      {{ item }}
    </button>

    <button
      v-if="continuesPage.end < totalPage"
      @click="$emit('getPageNo', pageNo + 1)"
    >
      ···
    </button>
    <button
      v-if="continuesPage.end < totalPage"
      @click="$emit('getPageNo', totalPage)"
      :class="{ active: pageNo === totalPage }"
    >
      {{ totalPage }}
    </button>
    <button
      :disabled="pageNo === totalPage"
      @click="$emit('getPageNo', pageNo + 1)"
    >
      下一页
    </button>

    <button style="margin-left: 30px">共 {{ total }} 条</button>
  </div>
</template>

<script>
export default {
  name: "Pagination",
  //pageNo:当前页码
  //pageSize:单页数量
  //total:总页数
  //continues:中间的连续页码数
  props: ["pageNo", "pageSize", "total", "continues"],
  computed: {
    //计算总共多少页
    totalPage() {
      //通过向上取证获取除不尽后的最大值
      return Math.ceil(this.total / this.pageSize);
    },
    //计算连续页码
    continuesPage() {
      let start = 0; //设置连续分页的起始页
      let end = 0; //设置连续分页的结束页
      var newArr = [];
      //如果连续页码的数量大于计算出的总页数的话(非正常情况)
      if (this.continues > this.totalPage) {
        //那就直接从头展示到尾就行了
        start = 1;
        end = this.totalPage;
      } else {
        //正常情况下连续页码小于总页数
        //获取当前元素的前两个或者后两个
        //为了动态获取起始值
        start = this.pageNo - parseInt(this.continues / 2);
        //动态获取结束的值
        end = this.pageNo + parseInt(this.continues / 2);
        //当计算出的开始页码数小于1的时候就给开始页码赋值为1,结束页码赋值为连续页码数
        if (start < 1) {
          start = 1;
          end = this.continues;
        }
        //如果计算出的end大于总页码数的话那就将初始值往前推一位，结束值赋值为最后一页
        if (end > this.totalPage) {
          start = end - this.continues - 1;
          end = this.totalPage;
        }
      }
      for (let i = start; i <= end; i++) {
        newArr.push(i);
      }
      //同时返回三个值需要用到对象
      return { newArr, start, end };
    },
  },
};
</script>

<style lang="less" scoped>

.pagination {
  text-align: center;
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #e1251b;
      color: #fff;
    }
  }
}
</style>